package com.audiorecorder;

import android.media.AudioFormat;
import android.os.Bundle;

import com.MyApplication;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.myatest.R;
import com.pbase.tools.Logger;
import com.zlw.main.recorderlib.RecordManager;
import com.zlw.main.recorderlib.recorder.RecordConfig;
import com.zlw.main.recorderlib.recorder.RecordHelper;
import com.zlw.main.recorderlib.recorder.listener.RecordDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordFftDataListener;
import com.zlw.main.recorderlib.recorder.listener.RecordResultListener;
import com.zlw.main.recorderlib.recorder.listener.RecordSoundSizeListener;
import com.zlw.main.recorderlib.recorder.listener.RecordStateListener;

import java.io.File;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * User: psn
 * Date: 2021/10/28
 * 测试录音
 */
public class AudioTestActivity extends AppCompatActivity   {

    private File mRsult;
    private AudioPlayer mAudioPlayer;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_audiotest);
        XXPermissions.with(this)
                .permission(Permission.RECORD_AUDIO)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {

                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {

                    }
                });
        // * 当前v1.08版本只能mp3其他格式因为临时文件无法更改存储位置导致存储权限问题，无法录制
        //初始化
        RecordManager.getInstance().init(MyApplication.getInstance(), true);
        //修改录音文件存放位置（默认sdcard/Record）
        RecordManager.getInstance().changeRecordDir(this.getFilesDir().toString());
        //修改录音格式(默认:WAV)
        RecordManager.getInstance().changeFormat(RecordConfig.RecordFormat.WAV);
        //修改录音配置
        //采样率
        RecordManager.getInstance().changeRecordConfig(RecordManager.getInstance().getRecordConfig().setSampleRate(16000));
        //位宽
        RecordManager.getInstance().changeRecordConfig(RecordManager.getInstance().getRecordConfig().setEncodingConfig(AudioFormat.ENCODING_PCM_8BIT));
        //录音状态监听
        RecordManager.getInstance().setRecordStateListener(new RecordStateListener() {
            @Override
            public void onStateChange(RecordHelper.RecordState state) {
                Logger.d(getClass(), "onStateChange state " + state);
            }

            @Override
            public void onError(String error) {
                Logger.d(getClass(), "onError error " + error);
            }
        });
        //录音结果监听
        RecordManager.getInstance().setRecordResultListener(new RecordResultListener() {
            @Override
            public void onResult(File result) {
                Logger.d(getClass(), "onResult onResult " + result);
                mRsult = result;
            }
        });
        //声音大小监听
        RecordManager.getInstance().setRecordSoundSizeListener(new RecordSoundSizeListener() {
            @Override
            public void onSoundSize(int soundSize) {
                Logger.d(getClass(), "onSoundSize soundSize " + soundSize);
            }
        });
        //音频数据监听
        RecordManager.getInstance().setRecordDataListener(new RecordDataListener() {
            @Override
            public void onData(byte[] data) {
            }
        });
        //音频可视化数据监听
        RecordManager.getInstance().setRecordFftDataListener(new RecordFftDataListener() {
            @Override
            public void onFftData(byte[] data) {
//                audioView.setWaveData(data);
            }
        });

        //-录音-----------------------
        findViewById(R.id.btn_audio_start).setOnClickListener(v -> {
            RecordManager.getInstance().start();
        });
        findViewById(R.id.btn_audio_pause).setOnClickListener(v -> {
            RecordManager.getInstance().pause();

        });
        findViewById(R.id.btn_audio_restart).setOnClickListener(v -> {
            RecordManager.getInstance().resume();
        });
        findViewById(R.id.btn_audio_stop).setOnClickListener(v -> {
            RecordManager.getInstance().stop();
        });

        mAudioPlayer = new AudioPlayer();
        getLifecycle().addObserver(mAudioPlayer);
        //播放------------------------
        findViewById(R.id.btn_play_start).setOnClickListener(v -> {
            mAudioPlayer.start(mRsult.toString(), () -> {
                Logger.d(getClass(), "Player ok");
            });
        });
        findViewById(R.id.btn_play_pause).setOnClickListener(v -> {
            mAudioPlayer.pause();

        });
        findViewById(R.id.btn_play_restart).setOnClickListener(v -> {
            mAudioPlayer.reStart();
        });
        findViewById(R.id.btn_play_stop).setOnClickListener(v -> {
            mAudioPlayer.stop();
        });
    }


}
